package com.hustzp.com.xichuangzhu;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.preference.PreferenceManager;

import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.hustzp.com.xichuangzhu.demo.business.Student;
import com.hustzp.com.xichuangzhu.poetry.model.LikePost;
import com.hustzp.com.xichuangzhu.poetry.model.PostComment;
import com.hustzp.com.xichuangzhu.poetry.model.UserNotification;
import com.hustzp.com.xichuangzhu.utils.BrightnessUtil;
import com.hustzp.com.xichuangzhu.utils.PreferenceKey;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.umeng.socialize.PlatformConfig;

/**
 * Created by fujiangren on 16/5/25.
 */
public class XichuangzhuApplication extends Application implements PreferenceKey {

    private static XichuangzhuApplication ourInstance = null;
    private SharedPreferences xichuangzhuSp;

    public static final String FONT_NAME_HUAWENFANGSONG = "fonts/huawenfangsong.ttf";
    public static final String FONT_NAME_WYUE = "fonts/Wyue.otf";

    private Typeface typefaceHuaWenWyue = null;
    private Typeface typefaceHuaWenFangSong = null;

    public TrChangeListener getTrChangeListener() {
        return trChangeListener;
    }

    public void setTrChangeListener(TrChangeListener trChangeListener) {
        this.trChangeListener = trChangeListener;
    }

    private TrChangeListener trChangeListener = null;

    public TextTypefaceChangeListener getTextTypefaceChangeListener() {
        return textTypefaceChangeListener;
    }

    public void setTextTypefaceChangeListener(TextTypefaceChangeListener textTypefaceChangeListener) {
        this.textTypefaceChangeListener = textTypefaceChangeListener;
    }

    private TextTypefaceChangeListener textTypefaceChangeListener = null;

    public Typeface getTypefaceHuaWenWyue() {
        return typefaceHuaWenWyue;
    }

    public void setTypefaceHuaWenWyue(Typeface typefaceHuaWenWyue) {
        this.typefaceHuaWenWyue = typefaceHuaWenWyue;
    }

    public Typeface getTypefaceHuaWenFangSong() {
        return typefaceHuaWenFangSong;
    }

    public void setTypefaceHuaWenFangSong(Typeface typefaceHuaWenFangSong) {
        this.typefaceHuaWenFangSong = typefaceHuaWenFangSong;
    }


    public static XichuangzhuApplication getInstance() {


        return ourInstance;
    }


    public static void setOurInstance(XichuangzhuApplication ourInstance) {
        XichuangzhuApplication.ourInstance = ourInstance;
    }

    public SharedPreferences getXichuangzhuSp() {
        return xichuangzhuSp;
    }

    public void setXichuangzhuSp(SharedPreferences xichuangzhuSp) {
        this.xichuangzhuSp = xichuangzhuSp;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        ourInstance = this;
        this.xichuangzhuSp = PreferenceManager.getDefaultSharedPreferences(this);
        AVOSCloud.initialize(this,
                "9pq709je4y36ubi10xphdpovula77enqrz27idozgry7x644", "364g5iu6q1f3hhwz8wruvmayu3vsap995wh4ws7tszor43np");
        AVObject.registerSubclass(Student.class);
        AVObject.registerSubclass(LikePost.class);
        AVObject.registerSubclass(PostComment.class);
        AVObject.registerSubclass(UserNotification.class);
        AVOSCloud.setDebugLogEnabled(true);
        initImageLoader(this);

        typefaceHuaWenFangSong = Typeface.createFromAsset(getResources().getAssets(), FONT_NAME_HUAWENFANGSONG);
        typefaceHuaWenWyue = Typeface.createFromAsset(getResources().getAssets(), FONT_NAME_WYUE);

        // umeng-share-config
        PlatformConfig.setWeixin("wx36788a7b5c2fe0a5", "35cfefbc8a56f0c772a7185601adcf36");
        PlatformConfig.setSinaWeibo("4038385410","6aa1c5183b907646e3325dc6e3e2c745");

//        String test ="任付江";
//        char[] testCharArray = test.toCharArray();
//        String verticalChar = "";
//        String aNewLine = "\r\n";
//        for(int i = 0;i < testCharArray.length;i++){
//
//
//
//            verticalChar = verticalChar +testCharArray[i]+aNewLine;
//            Log.e("verticalChar", verticalChar);
//
//        }


    }


    public synchronized static void initImageLoader(Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context).threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCacheSize(1024 * 1024 * 300)
                .memoryCache(new WeakMemoryCache())
                .memoryCacheSize((int) (Runtime.getRuntime().maxMemory() / 3))
                // .diskCache(new UnlimitedDiskCache(HCZ_CACHEDIR))
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs().build();
        ImageLoader.getInstance().setStart_MAA(false);
        ImageLoader.getInstance().init(config);
    }

    public String lastviewId() {

        return this.xichuangzhuSp.getString(KEY_LAST_REVIEW_KEY, "");

    }

    // 1 简体 2 繁体
    public String getFanjian() {

        return this.xichuangzhuSp.getString(KEY_TRADITIONAL_KEY, "1");

    }

    public void saveFanjian(String value) {
        this.xichuangzhuSp.edit().putString(KEY_TRADITIONAL_KEY, value).commit();
    }


    public void saveLastviewId(String reviewId) {
        this.xichuangzhuSp.edit().putString(KEY_LAST_REVIEW_KEY, reviewId).commit();
    }

    // 0 文悦古体仿宋 1 华文仿宋
    public int getFontStyle() {
        return this.xichuangzhuSp.getInt(KEY_SETTING_FONT_STYLE, 0);

    }

    public void saveFontStyle(int value) {
        this.xichuangzhuSp.edit().putInt(KEY_SETTING_FONT_STYLE, value).commit();
    }


    // 亮度
    public int getBrightness() {
        return this.xichuangzhuSp.getInt(KEY_SETTING_BRIGHT_NEWSS, BrightnessUtil.getScreenBrightness(this));

    }

    public void saveBrightness(int value) {
        this.xichuangzhuSp.edit().putInt(KEY_SETTING_BRIGHT_NEWSS, value).commit();
    }

    // 0 关闭推送通知 1 开启推送通知
    public boolean getPushMsgCfg() {
        return this.xichuangzhuSp.getBoolean(KEY_SETTING_PUSH_MSG, false);

    }

    public void savePushMsgCfg(boolean value) {
        this.xichuangzhuSp.edit().putBoolean(KEY_SETTING_PUSH_MSG, value).commit();
    }

    public void textTypefaceChangeNotify() {
        if (this.getTextTypefaceChangeListener() != null) {
            this.getTextTypefaceChangeListener().OnTextTypefaceChange();
        }
    }

    public void textTrChangeNotify() {
        if (this.getTrChangeListener() != null) {
            this.getTrChangeListener().OnTextrChange();
        }
    }

    public interface TextTypefaceChangeListener {

        public void OnTextTypefaceChange();
    }

    public interface TrChangeListener {

        public void OnTextrChange();
    }


    // 评论草稿
    public String getCommentDraft() {
        return this.xichuangzhuSp.getString(KEY_COMMENT_DRAFT, "");

    }

    public void saveCommentDraft(String value) {
        this.xichuangzhuSp.edit().putString(KEY_COMMENT_DRAFT, value).commit();
    }

}
